<!--数据字典管理-->
<template>
  <div>
    <a-card>
      <p slot="title">
        检索条件
      </p>
      <div class="formSearch">
        <a-form ref="lessonDTO" :label-width="80" layout="inline"
                @keydown.native.enter.prevent="handleSubmit">
          <a-form-item label="课堂名称：" prop="searchKey">
            <a-input v-model="searchKey" placeholder="请输入课堂名称"
                     @keyup="formatEnterContent(searchKey)"
                     @blur="formatEnterContent(searchKey)" style="width: 220px">
              <a-icon slot="prefix" type="user" class="displayPre" />
              <a-icon v-if="searchKey" slot="suffix" type="close-circle" @click="emitEmpty(searchKey)" />
            </a-input>
          </a-form-item>
          <a-button type="primary" @click="handleSubmit"
                    >查询
          </a-button>
          <a-button style="margin-left: 8px" @click="reStore">重置</a-button>
        </a-form>
      </div>
    </a-card>
    <div style="height: 10px"></div>
    <div class="commonStyle nyContent">
      <div class="table-operator" v-if="hasPermission ('classT:save')">
        <a-button type="primary"  @click="addDTO" >新增</a-button>
      </div>
      <a-table :columns="columns" :dataSource="lessonManagePageData" :rowKey="record => record.recordId"
               :pagination="false">
                <span slot="index" slot-scope="text, record, index">
                    {{ (page.currentPage - 1) * page.pageSize + index + 1 }}
                </span>
        <span slot="scheduleTime" slot-scope="text, record, index">
                    {{ moment(record.date).format("YYYY-MM-DD") + " " + record.attendTime + '-'+ record.finishTime}}
                </span>
        <span slot="realStartTime" slot-scope="text, record, index">
                    {{ record.realStartTime}}
                </span>
        <span slot="state" slot-scope="text, record, index" :style="{color:(text === '1'?'#00FF00':(text === '2'?'#ff0000':'#000000'))}">
                    {{ lessonStates[Number(text)].label}}
                </span>
        <span slot="operation" slot-scope="text, record, index" class="table-operation">
                         <a @click="editDTO(record)" v-if="hasPermission ('classT:save')">
                            编辑</a>
          <a @click="showCourseDesc(record)" >
                            实验简介</a>
          <a @click="startLessonSign(record)" v-if="hasPermission ('classT:startSign')">
                            开启签到</a>
          <a @click="stuSign(record)" v-if="hasPermission ('classT:signIn:sign')">
                            签到</a>
          <a @click="stuLineUp(record)" v-if="hasPermission ('classT:lineUp:save')">
                            提交排队</a>
          <a @click="showSignList(record)" v-if="hasPermission ('classT:signIn:list')">
                            签到列表</a>
          <a @click="exportScore(record)" v-if="isTeacher ||isSuperAdmin || isAdmin">
                            导出成绩</a>
          <a @click="showLineUp(record)" v-if="hasPermission ('classT:lineUp:page')">
                            排队查看</a>
          <a @click="showLabEvolve(record)" v-if="hasPermission ('classT:lineUp:evolve')">
                            实验进展</a>
          <a @click="showAnswerList(record)" v-if="hasPermission ('classT:aq:page')">
                            课堂答疑</a>
          <a @click="endLesson(record)" v-if="hasPermission ('classT:endClasst')">
                            结束课程</a>
          <a @click="showSuggestList(record)" v-if="hasPermission ('classT:suggest:page')">
                            课后建议</a>
          <a @click="showMyScore(record)" v-if="(isStudent || isSuperAdmin) && hasPermission ('score:personal')">
                            我的成绩</a>
          <a @click="lessonScoreAutoCalc(record)" v-if="record.scoreCalcWay === '2' && hasPermission ('classT:auto:calculate')">
                            自动计算成绩</a>
                        <a @click="deleteDTO(record)" v-if="hasPermission ('classT:delete')">
                                          删除</a>
                </span>
      </a-table>
      <Page :totalCount="pageVal.total" :currentPage="pageVal.currentPage" ref="page"
            :pageSize="pageVal.pageSize" @changePage="changePage" @changePageSize="changePageSize" />
    </div>
    <add-edit ref="addEdit"
              :objDTO="objDTO" @init="init" />
    <sign-edit ref="signEdit"
               @init="init" />
    <sign-list ref="signList"
               @init="init" />
    <line-up-edit ref="lineUpEdit"
               @init="init" />
    <line-up-list ref="lineUpList" />
    <lab-evolve-list ref="labEvolveList" />
    <answer-list ref="answerList" />
    <suggest-list ref="suggestList" />
    <my-score ref="myScore" />
    <course-description ref="courseDescription" />
    <iframe id="exportScore" style="display: none"></iframe>
  </div>
</template>

<script>
import addEdit from './addEdit.vue'
import signEdit from './signEdit.vue'
import signList from './signList.vue'
import myScore from './myScore.vue'
import lineUpEdit from './lineUpEdit.vue'
import courseDescription from './courseDescription.vue'
import labEvolveList from './labEvolveList.vue'
import lineUpList from '../lineUpManage/lineUpList.vue'
import answerList from '../answerManage/answerList.vue'
import suggestList from '../suggestManage/suggestList.vue'
import { mapState } from 'vuex'
import Page from 'module/pagination'
import Util from '@/libs/util';
import moment from "moment";
export default {
  name: 'lessonManage',
  components: {
    suggestList,
    courseDescription,
    addEdit,
    myScore,
    signEdit,
    signList,
    lineUpEdit,
    lineUpList,
    labEvolveList,
    answerList,
    Page,
  },
  data () {
    return {
      title: true,
      searchKey: '',
      selectedNode: {id: '', name: ''},
      array: [ 10, 20, 30, 40 ],
      /*选中的记录*/
      selectedRowKeys: [],
      isSuperAdmin:sessionStorage.getItem('user') === 'superAdmin',
      isAdmin:sessionStorage.getItem('user') === 'admin',
      isStudent:sessionStorage.getItem('roleIds').indexOf('bf87a2d6702c3ee927f113806e7a9736') > 0,
      isTeacher:sessionStorage.getItem('roleIds').indexOf('c2eb3ed226c8d4f52d9203ead594817d') > 0,
      objDTO: {
        recordId: '',
        name: '',
        buildName: '',
        buildCode: '',
        roomCode: '0',
        diskNum: '',
      },
      lessonStates:[
        {value:'0',label:"未开始"},
        {value:'1',label:"进行中"},
        {value:'2',label:"已结束"},
      ],
      page: {
        currentPage: 1,
        pageSize: 10
      },
      columns: [
        {title: '序号', key: 'index', width: '8%', align: 'center', scopedSlots: {customRender: 'index'}},
        {title: '实验名称', dataIndex: 'name', key: 'name', width: '10%'},
        {title: '所在实验室', dataIndex: 'classroomName', key: 'classroomName', width: '10%'},
        {title: '上课时间段', dataIndex: 'date', key: 'date', width: '15%', scopedSlots: {customRender: 'scheduleTime'}},
        {title: '实际上课时间', dataIndex: 'realStartTime', key: 'realStartTime', width: '15%', scopedSlots: {customRender: 'realStartTime'}},
        {title: '状态', dataIndex: 'state', key: 'state', width: '8%', scopedSlots: {customRender: 'state'}},
        {title: '上课教师', dataIndex: 'teacherName', key: 'teacherName', width: '8%',},
        {title: '操作', key: 'operation', width: '16%', scopedSlots: {customRender: 'operation'}}
      ],
    }
  },
  computed: {
    moment() {
      return moment
    },
    lessonManagePageData () {
      return this.$store.state.lessonManage.lessonManagePageData
    },
    ...mapState ({
      pageVal: state => state.lessonManage.page,
    }),

  },
  filters: {
    filterWord(value, list) {
      for (let i=0; i<list.length; i++) {
        if (value === list[i].value) {
          return list[i].label
        }
      }
    },
  },
  methods: {
    exportScore (record) {
      document.getElementById ('exportScore').src = '/lab/score/export?courseId='+record.courseId;
    },
    emitEmpty () {
      this.searchKey = ''
      this.init ()
    },
    reStore(){
      this.searchKey = ''
      this.page.currentPage = 1
      this.init();
    },

    editDTO(Obj){
      this.objDTO = {...Obj}
      this.title = false
      this.$refs.addEdit.showEdit(this.objDTO)
    },
    addDTO(){
      this.clearDTO ()
      this.title = true
      this.$refs.addEdit.showModal ()
    },
    clearDTO(){
      this.objDTO= {
        recordId: '',
            name: '',
            buildName: '',
            buildCode: '',
            roomCode: '0',
            diskNum: '',
      }
    },
    /*
     * 过滤输入框输入的特殊字符
     * */
    formatEnterContent ( value ) {
      let a = Util.clearSpecialChart(value);
      this.searchKey = a
    },

    /**
     * Function    : init
     * Description : 初始化列表
     * Input       : none
     * Return      : none
     */
    init () {
      let para = Object.assign ({},
          {name: this.searchKey.trim ()}, this.page)
      this.$store.dispatch ('getLessonManagePageListAct', para)
      this.$store.commit ('setLessonManageQueryPrams', para)
    },



    /**
     * Function    : delRole
     * Description : 批量删除角色
     * Input       : none
     * Return      : none
     */
    deleteDTO (obj) {
      this.$confirm ({
        title: '确认删除【'+obj.name+'】吗？',
        okText: '确定',
        cancelText: '取消',
        onOk: () => {
          let _this = this
          this.$store.dispatch ('deleteLessonAct', {recordId: obj.recordId}).then (response => {
            if (response.code === 0) {
              _this.init ()
              this.$message.success (response.message)
            } else {
              this.$message.error (response.message)
            }
          })
        },
      })
    },


    startLessonSign (obj) {
      this.$confirm ({
        title: '确认开启【'+obj.name+'】课堂的签到吗？',
        okText: '确定',
        cancelText: '取消',
        onOk: () => {
          let _this = this
          this.$store.dispatch ('startLessonSignAct', {recordId: obj.recordId}).then (response => {
            if (response.code === 0) {
              _this.init ()
              this.$message.success (response.message)
            } else {
              this.$message.error (response.message)
            }
          })
        },
      })
    },

    endLesson (obj) {
      this.$confirm ({
        title: '确认结束【'+obj.name+'】的课堂吗？',
        okText: '确定',
        cancelText: '取消',
        onOk: () => {
          let _this = this
          this.$store.dispatch ('endLessonAct', {recordId: obj.recordId}).then (response => {
            if (response.code === 0) {
              _this.init ()
              this.$message.success (response.message)
            } else {
              this.$message.error (response.message)
            }
          })
        },
      })
    },

    lessonScoreAutoCalc (obj) {
      this.$confirm ({
        title: '确认自动计算【'+obj.name+'】课堂的成绩吗？',
        okText: '确定',
        cancelText: '取消',
        onOk: () => {
          let _this = this
          this.$store.dispatch ('lessonScoreAutoCalcAct', {recordId: obj.recordId}).then (response => {
            if (response.code === 0) {
              _this.init ()
              this.$message.success (response.message)
            } else {
              this.$message.error (response.message)
            }
          })
        },
      })
    },

    stuSign (obj) {
      this.$refs.signEdit.showModal (obj)
    },
    stuLineUp (obj) {
      this.$refs.lineUpEdit.showModal (obj)
    },
    showSignList (obj) {
      this.$refs.signList.showModal (obj)
    },
    showSignCount (obj) {
      this.$refs.signStatistic.showModal (obj)
    },
    showLineUp (obj) {
      this.$refs.lineUpList.showModal (obj)
    },
    showCourseDesc (obj) {
      this.$refs.courseDescription.showModal (obj.description)
    },
    showLabEvolve (obj) {
      this.$refs.labEvolveList.showModal (obj)
    },
    showAnswerList (obj) {
      this.$refs.answerList.showModal (obj)
    },
    showSuggestList (obj) {
      this.$refs.suggestList.showModal (obj)
    },
    showMyScore (obj) {
      this.$refs.myScore.showModal (obj)
    },


    /**
     * Function    : changePage
     * Description : 切换页码
     * Input       : val
     * Return      : none
     */
    changePage ( val ) {
      this.page.currentPage = val
      this.init ()
    },

    /**
     * Function    : changePageSize
     * Description : 切换每页条数
     * Input       : vm
     * Return      : none
     */
    changePageSize ( vm ) {
      this.page.pageSize = vm
      this.page.currentPage = 1
      this.init ()
    },

    /**
     * Function    : handleSubmit
     * Description : 查询按钮
     * Input       : none
     * Return      : none
     */
    handleSubmit () {
      this.page.currentPage = 1
      this.init ()
    },


  },
  created () {
    this.searchKey = this.$route.query.searchKey === undefined ? '' : this.$route.query.searchKey
    this.page.currentPage = this.$route.query.currentPage === undefined ? 1 : this.$route.query.currentPage
    this.page.pageSize = this.$route.query.pageSize === undefined ? 10 : this.$route.query.pageSize
    this.init ()
    console.log(sessionStorage.getItem('roleIds').indexOf(""))
    console.log(sessionStorage.getItem('user'))
  },
  mounted () {

  }
}
</script>
